//  Inside Microsoft Exchange 2007 Web Services
//  Chapter 5
//
//  Copyright (c) 2007 David Sterling, Ben Spain, Mark Taylor, Huw Upshall, Michael Mainer.
//  All rights reserved.
//
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Xml;
using System.IO;
using ProxyHelpers.EWS;

namespace FindItemWithSorting
{
    class Program
    {
        static void Main(string[] args)
        {
            ExchangeServiceBinding binding = new ExchangeServiceBinding();
            binding.Url = @"https://yourServer/ews/Exchange.asmx";
            binding.Credentials = new NetworkCredential(
                "username",
                "password",
                "domain");

            // Create some items to sort
            //
            ItemType item1 = BuildItemWithImportance("Message A", ImportanceChoicesType.Low);
            ItemType item2 = BuildItemWithImportance("Message B", ImportanceChoicesType.High);
            ItemType item3 = BuildItemWithImportance("Message C", ImportanceChoicesType.Low);
            ItemType item4 = BuildItemWithImportance("Message D", ImportanceChoicesType.High);
            ItemType item5 = BuildItemWithImportance("Message E", ImportanceChoicesType.Low);
            ItemType item6 = BuildItemWithImportance("Message F", ImportanceChoicesType.High);

            // Call helper method to create these items.
            //
            ItemIdType[] itemIds = binding.CreateBatchedItems(
                new ItemType[] { item1, item2, item3, item4, item5, item6 });

            // Now sort them
            //
            SortItems(binding);
        }

        /// <summary>
        /// Helper method to create our items..
        /// </summary>
        /// <param name="subject">Subject for item</param>
        /// <param name="importance">Importance for item</param>
        /// <returns>Created ItemType</returns>
        /// 
        private static ItemType BuildItemWithImportance(
                                        string subject,
                                        ImportanceChoicesType importance)
        {
            ItemType result = new ItemType();
            result.Subject = subject;
            result.Importance = importance;
            result.ImportanceSpecified = true;
            return result;
        }

        /// <summary>
        /// Sort items by importance and date time created (Listing 5-24)
        /// </summary>
        /// <param name="binding">binding to use for FindItem call</param>
        /// 
        public static void SortItems(ExchangeServiceBinding binding)
        {
            FindItemType request = new FindItemType();
            request.ItemShape = new ItemResponseShapeType();
            request.ItemShape.BaseShape = DefaultShapeNamesType.AllProperties;
            DistinguishedFolderIdType drafts = new
                  DistinguishedFolderIdType(DistinguishedFolderIdNameType.drafts);
            request.ParentFolderIds = new BaseFolderIdType[] { drafts };

            // create our sort order using our handy overloaded FieldOrderType 
            // constructor
            //
            request.SortOrder = new FieldOrderType[]{
                new FieldOrderType(
                    SortDirectionType.Ascending, 
                    new PathToUnindexedFieldType(
                        UnindexedFieldURIType.itemImportance)),
                new FieldOrderType(
                    SortDirectionType.Descending, 
                    new PathToUnindexedFieldType( 
                        UnindexedFieldURIType.itemDateTimeCreated))
            };

            FindItemResponseType response = binding.FindItem(request);
            FindItemResponseMessageType responseMessage =
                     response.ResponseMessages.Items[0] as
                              FindItemResponseMessageType;

            ItemType[] items = (responseMessage.RootFolder.Item as
                      ArrayOfRealItemsType).Items;
            foreach (ItemType item in items)
            {
                Console.WriteLine("Subject: " + item.Subject);
            }
        }
    }
}
